﻿module Sustainalytics.Governance {

    import Common = Sustainalytics.Common;
    import Company = Sustainalytics.Company;
    import Research = Sustainalytics.Research;
    import LRTM = Sustainalytics.LRTM;
    

    export class GovernanceProfileController extends Research.ResearchPageController {


        // whether current view is still loading
        public isInitLoading: boolean;

        // employee in charge with respective industry
        public companyGovernanceProfile: GovernanceProfileDto;
        
        public notFoundMessage: string;
        public noClaims: boolean;

        constructor(
            protected $scope: Company.ICompanyBasedScope,
            protected $q: ng.IQService,
            protected $sce: ng.ISCEService,
            protected $timeout: ng.ITimeoutService,
            protected $location: ng.ILocationService,
            protected $routeParams: ng.route.IRouteParamsService,
            protected CompanyService: Company.ICompanyService,
            protected GovernanceProfileService: IGovernanceProfileService,
            protected LongRunningTaskManagerService: LRTM.LongRunningTaskManagerService) {

            super($scope, $q, $location, $routeParams, CompanyService);

            var ctx = this;

            //$scope['governanceProfile'] = ctx;

            ctx.init();
        }

        // init logic
        private init(): ng.IPromise<any> {
            var ctx = this;

            ctx.isInitLoading = true;

            var def = ctx.$q.defer();

            var initPromise = super.initPage();

            // set EsgSummary view as the default view
            initPromise.then(
                // success
                function () {
                    ctx.$timeout(function () {
                        ctx.$scope.$apply(function () {

                            // get industry lead personnel
                            ctx.getCompanyGovernanceProfile().then(
                                // success
                                function () {
                                    def.resolve();
                                },
                                // error
                                function () {
                                    def.reject();
                                });
                        });
                    });
                },
                // error
                function (reason) {
                    def.reject(reason);
                });

            var promise = def.promise;

            promise['finally'](function () {
                ctx.isInitLoading = false;
            });

            return promise;
        }

        // init - get company governance profile
        private getCompanyGovernanceProfile(): ng.IPromise<GovernanceProfileDto> {
            var ctx = this;

            var promise = ctx.GovernanceProfileService.getGovernanceProfile(ctx.company.id);

            promise.then(function (res: any) //GovernanceProfileDto or 7000 or 7001
            {
                if (res.errCode != undefined)
                {
                    ctx.notFoundMessage = res.message;
                }
                else
                {
                    ctx.$timeout(function () {
                        ctx.$scope.$apply(function () {
                            ctx.companyGovernanceProfile = res;
                        });
                    });   
                }
            },
                (reason) =>
                {
                    if (reason.status == 401)
                    {
                        ctx.noClaims = true;
                    }
                });

            return promise;
        }


        public DownloadGovernancePDF(fileId: string, fileName: string) {
            var ctx = this;
            var taskNumber = 202;
            var data = { fileId: fileId, fileName: fileName };
            var promise = ctx.LongRunningTaskManagerService.runTask(fileName + "_" + moment().format("DD-MM-YYYY"), JSON.stringify(data), taskNumber, true, null);
        }

        
        public DownloadGovernanceFileEnabled(fileId: string, fileName: string): boolean {
            var ctx = this;
            var result = false;

            if (fileId != undefined && fileName != undefined) {
                result = true;
            }

            return result;
        }


        public RenderHTML(htmlCode: string) {
            var ctx = this;
            return ctx.$sce.trustAsHtml(htmlCode);
        }

    }

    angular.module(governanceModuleName).controller('GovernanceProfileController', GovernanceProfileController);

} 